/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
Ext.onReady(function(){

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var val = document.URL.split("=");
    //var bd = Ext.getBody();

    /*
     * ================  Simple form  =======================
     */
    //bd.createChild({tag: 'h2', html: 'Stop Form'});


    var stop_form = new Ext.form.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        url:'editStop.jsp',
        frame:true,
        title: 'Stop',
        bodyStyle:'padding:5px 5px 0',
        width: 320,
        defaults: {width: 230}
    });

    var stop_id = new Ext.form.TextField({
       fieldLabel: 'ID',
       name: 'id',
       width: 190,
       allowBlank: false
    });
    stop_form.items.add(stop_id);

    var stop_name = new Ext.form.TextField({
       fieldLabel: 'Name',
       name: 'name',
       width: 190,
       allowBlank: false
    });
    stop_form.items.add(stop_name);

    var stop_desc = new Ext.form.TextField({
       fieldLabel: 'Description',
       name: 'description',
       width: 190,
       allowBlank: false
    });
    stop_form.items.add(stop_desc);

    var stop_lat = new Ext.form.NumberField({
       fieldLabel: 'latitude',
       name: 'latitude',
       width: 190,
       allowNegative: true,
       decimalPrecision: 10,
       allowBlank: false
    });
    stop_form.items.add(stop_lat);

    var stop_long = new Ext.form.NumberField({
       fieldLabel: 'Longitude',
       name: 'longitude',
       width: 190,
       allowNegative: true,
       decimalPrecision: 10,
       allowBlank: false
    });
    stop_form.items.add(stop_long);

    var conn = new Ext.form.Checkbox({
        fieldLabel: '',
        name: 'connected',
        boxLabel: 'Connected'
    });
    stop_form.items.add(conn);
    
    if (val.length > 1) {
       stop_form.form.load({
            url:'getStop.jsp?id='+val[1],
            method:'GET',
            waitMsg:'Loading',
            success: function (form, action) {

            },
            failure: function (form, action) {
                Ext.MessageBox.alert('Message','Error retrieving Stop with ID ' + val[1]);

            }
        });
    }

    var cancel = stop_form.addButton({
        text: 'Close',
        disabled:false,
        handler: function(){
            window.location.href='listStops.jsp';
        }
    });

    var submit = stop_form.addButton({
        text: 'Save',
        disabled:false,
        handler: function(){
            
            stop_form.form.submit({
                url:'saveStop.jsp',
                method: 'POST',
                waitMsg:'Saving Data...',
                success: function (form, action) {
                    Ext.MessageBox.alert('Message', 'Saved OK');
                },
                failure:function(form, action) {
                    Ext.MessageBox.alert('Message', 'Save failed <p>' + action.response.responseText);
                }
            });
            
        }
    });

    stop_form.render(document.body);
});